/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jonawer;

import java.awt.event.WindowAdapter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.TimerTask;

/**
 *
 * @author przemk
 */
public class GPSd {

    protected Socket gpsSocket;
    protected BufferedReader gpsBR;
    protected BufferedWriter gpsBW;
    protected String nmeaSentence;
    protected boolean gpsOK;
    protected String gpstext;
    protected String gpsDate;
    protected String gpsLatitude;
    protected String gpsLongitude;
    protected String gpsAltitude;
    protected String gpsMode;
    protected String gpsSatCount;
    protected String gpsErrPos;
    protected String gpsErrHoriz;
    protected String gpsErrVert;
    protected String gpsFix;
    protected String gpsTrack;
    protected String gpsSpeed;
    protected String gpsOnline;
    protected java.util.Timer timer;
    public boolean pozycjonuj;
    JonawerView aThis;

    /** Constructor
     */
    public GPSd() {
        gpsSocket = null;
        gpsBR = null;
        gpsOK = false;
        nmeaSentence = "Unititialised";
        gpsDate = "";
        gpsLatitude = "";
        gpsLongitude = "";
        gpsAltitude = "";
        gpstext = "";
        gpsMode = "";
        gpsSatCount = "";
        gpsErrPos = "";
        gpsErrHoriz = "";
        gpsErrVert = "";
        gpsFix = "";
        gpsTrack = "";
        gpsSpeed = "";
        gpsOnline = "";
        // Stuff to set up GUI here

        connectGPS();
        timer = new java.util.Timer();
        timer.schedule(new GPSTask(), 0, 5); // poll every 5 seconds
    }

    GPSd(JonawerView aThis) {
        gpsSocket = null;
        gpsBR = null;
        gpsOK = false;
        nmeaSentence = "Unititialised";
        gpsDate = "";
        gpsLatitude = "";
        gpsLongitude = "";
        gpsAltitude = "";
        gpstext = "";
        gpsMode = "";
        gpsSatCount = "";
        gpsErrPos = "";
        gpsErrHoriz = "";
        gpsErrVert = "";
        gpsFix = "";
        gpsTrack = "";
        gpsSpeed = "";
        gpsOnline = "";
        // Stuff to set up GUI here
        this.aThis = aThis;
        connectGPS();
        timer = new java.util.Timer();

        timer.schedule(new GPSTask(), 0, 10); // poll every 5 seconds

    }

    /**
     * @return the pozycjonuj
     */
    public boolean isPozycjonuj() {
        return pozycjonuj;
    }

    /**
     * @param pozycjonuj the pozycjonuj to set
     */
    public void setPozycjonuj(boolean pozycjonuj) {
        this.pozycjonuj = pozycjonuj;
    }

    class GPSTask extends TimerTask {

        public void run() {
            pollGPS();
        }
    }
    // All actual program code removed

    protected void connectGPS() {
        System.out.println("connectGPS");

        if (gpsSocket == null) {
            try {
                System.out.println("otwarcie socketu");
                gpsSocket = new Socket(InetAddress.getByName("localhost"), 2947);
                gpsOK = true;
                nmeaSentence = "gpsd Initialised";
                gpsBR = new BufferedReader(new InputStreamReader(gpsSocket.getInputStream()));
                gpsBW = new BufferedWriter(new OutputStreamWriter(gpsSocket.getOutputStream()));
                gpsBW.write("w+r-\n");
                gpsBW.flush();
            } catch (Exception e) {
                gpsOK = false;
                nmeaSentence = "gpsd socket failure " + e;
                System.out.println("e:" + e.getLocalizedMessage());
            }
        }

        System.out.println("Connect GPS " + gpsOK + "\n");
    }

    protected void pollGPS() {

        if (gpsBR == null) {
            return;
        }

        try {
            while (gpsBR.ready()) {
                nmeaSentence = gpsBR.readLine();
                gpsOK = true;
                gpsDecode();
            }
        } catch (IOException ioe) {
            nmeaSentence = "I/O Error " + ioe;
            gpsOK = false;
        }

    }

    public void pozycjonuj(boolean doit) {
        if (doit) {
        } else {
        }
    }

    protected void gpsDecode() {
        String[] toks;
        String[] subtoks;
        char ctl;
        int i;
        int n;

        if (nmeaSentence.startsWith("GPSD")) {
            System.out.println("nmeaSentence:" + nmeaSentence);
            toks = nmeaSentence.split(" ");
            n = toks.length;
            try {
                if (toks[0].startsWith("GPSD,O=RMC") || toks[0].startsWith("GPSD,O=GGA") || toks[0].startsWith("GPSD,O=GSA")) {
                    System.out.println("gpsLatitude:  " + toks[3] + "\n" +
                            "gpsLongitude:  " + toks[4]);
                    if (isPozycjonuj()) {
                        aThis.setPosition(Float.valueOf(toks[3]).floatValue(),
                                Float.valueOf(toks[4]).floatValue());
                    }
                }
            } catch (Exception e) {
            }
        }

    }
    /**
    Annunciator Panel
     */
}

